import axios from 'axios'
import qs from 'qs'
import VueCookies from 'vue-cookies'

axios.defaults.withCredentials=true;

function setCookie(name, value, seconds) {
    seconds = seconds || 0;   //seconds有值就直接赋值，没有为0
    var expires = "";
    if (seconds != 0) {      //设置cookie生存时间
        var date = new Date();
        date.setTime(date.getTime() + (seconds * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expires + "; path=/";   //转码并赋值
}

function req(type, url, params, success, fail) {
	//setCookie('token','c725093b8e6ca698ac204c7c296458d1'); 
	//localStorage.setItem('token','c725093b8e6ca698ac204c7c296458d1')
	//setCookie('token',localStorage.getItem('token'));
	//alert(localStorage.getItem('token')+"------22");
	//VueCookies.set('token','c725093b8e6ca698ac204c7c296458d1');
	var urlStr;
		if(process.env.NODE_ENV=="development"){
			//urlStr="http://47.96.172.5:8082"+url;  //测试
		}else if(process.env.NODE_ENV=="production"){
			urlStr="https://admin.hzcckj.cn"+url;  //正式
		}
	
	//alert(document.cookie);
	axios({
		method: type, 
		url: urlStr,
		headers: {'token':localStorage.getItem('token')},
		//headers: {'token':"c725093b8e6ca698ac204c7c296458d1"},
		params: type == "get" ? qs.stringify(params) : '',
		data: type == "post" ? qs.stringify(params) : '',
	}).then(function(res) {
		if (success) {
			success(res.data);
		} else if (failure) {
			failure(res.data);
		} else {
			console.log('error: ' + JSON.stringify(res.data));
		}
	})
}



export default {
	get: function(url, params, success, fail) {
		return req('get', url, params, success, fail);
	},
	post: function(url, params, success, fail) {
		return req('post', url, params, success, fail);
	}
}
